import lodash from 'lodash';
import * as data from './data';
import log from '../../../log';
import * as util from '../../../util';

const likeParms = 'name'.split(',');
export function fetch(query, page, pageSize) {
    return util.resolve().then(() => {
        log.info('fetch activityType params----', query);
        const pageOpt = {
            page,
            pageSize
        };
        let where = [];
        if (lodash.keys(query).length > 0) {
            lodash.mapKeys(query, (value, key) => {
                if (likeParms.indexOf(key) >= 0) {
                    where.push([`${key}`, 'like', `%${value}%`]);
                } else {
                    where.push([`${key}`, '=', `${value}`]);
                }
            });
        }
        if (where.length < 1) {
            where = null;
        }
        return data.fetch(where, 1, pageOpt).then(res => {
            log.info('fetch activityType result----', res.toJSON());
            return util.data(res, 1);
        });
    }).catch(err => {
        log.error('fetch activityType is err----', err);
        return err;
    });
}
export function find(query) {
    return util.resolve().then(() => {
        log.info('find activityType params----', query);
        let where = [];
        if (lodash.keys(query).length > 0) {
            lodash.mapKeys(query, (value, key) => {
                if (likeParms.indexOf(key) >= 0) {
                    where.push([`${key}`, 'like', `%${value}%`]);
                } else {
                    where.push([`${key}`, '=', `${value}`]);
                }
            });
        }
        if (where.length < 1) {
            where = null;
        }
        return data.find(where, 1).then(res => {
            log.info('find activityType result----', util.data(res));
            return util.data(res);
        });
    }).catch(err => {
        log.error('find activityType is err----', err);
        return err;
    });
}
export function add(activityType) {
    return util.resolve().then(() => {
        log.info('add activityType params----', activityType);
        return data.find([{name: activityType.name}], 0).then(res => {
            if (res.toJSON().length > 0) {
                throw '分类名称重复！';
            }
            activityType.id = util.uuid();
            return data.add(activityType).then(res => {
                log.info('add activityType result----', util.data(res));
                return util.data(res);
            });
        });
    }).catch(err => {
        log.error('add activityType is err----', err);
        return err;
    });
}
export function edit(activityType) {
    return util.resolve().then(() => {
        log.info('edit activityType params----', activityType);
        return data.find([['id', '<>', `${activityType.id}`], ['name', '=', `${activityType.name}`]], 1).then(res => {
            if (res.toJSON().length > 0) {
                throw '分类名称重复！';
            }
            return data.edit(activityType).then(res => {
                log.info('edit activityType result----', util.data(res));
                return util.data(res);
            });
        });
    }).catch(err => {
        log.error('edit activityType is err----', err);
        return err;
    });
}
export function del(id) {
    return util.resolve().then(() => {
        log.info('delete activityType params----', id);
        return data.del(id).then(res => {
            log.info('delete activityType result----success', util.data(res));
            return util.data(res);
        });
    }).catch(err => {
        log.error('delete activityType is err----', err);
        return err;
    });
}
